package com.chinatsp.onlineapp.ui.main_menu;

import java.util.List;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.bean.WeatherService;
import com.chinatsp.onlineapp.bean.WeatherService.RespDataBean.ForecastConditionsBean;
import com.chinatsp.onlineapp.utils.WeatherType;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 主页面天气列表
 */

public class WeatherListAdapter extends BaseAdapter {
	private Context mContext;
	private List<WeatherService.RespDataBean.ForecastConditionsBean> weather;

	public WeatherListAdapter(Context context,
			List<WeatherService.RespDataBean.ForecastConditionsBean> areas) {
		mContext = context;
		this.weather = areas;
	}

	@Override
	public int getCount() {
		return weather == null ? 0 : weather.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.layout_weather_list_item, parent, false);
			holder = new ViewHolder();

			holder.tvWeatherWeek = (TextView) convertView
					.findViewById(R.id.tv_week);
			holder.ivWeatherShow = (ImageView) convertView
					.findViewById(R.id.iv_weather_img);
			holder.tvTemperature = (TextView) convertView
					.findViewById(R.id.tv_temperature);
			holder.tvWeatherType = (TextView) convertView
					.findViewById(R.id.tv_weather_type);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ForecastConditionsBean forecastConditionsBean = weather.get(position);
		holder.tvWeatherWeek.setText(forecastConditionsBean.getWeather_week());
		WeatherType.showImg(forecastConditionsBean.getWeather_type(), holder.ivWeatherShow);
		holder.tvWeatherType.setText(forecastConditionsBean.getWeather_condition());
        holder.tvTemperature.setText(forecastConditionsBean.getHigh_temperature());
		return convertView;
	}

	public void setData(
			List<WeatherService.RespDataBean.ForecastConditionsBean> data) {
		this.weather = data;
		notifyDataSetChanged();
	}

	static class ViewHolder {
		ImageView ivWeatherShow;
		TextView tvWeatherWeek;
		TextView tvWeatherType;
		TextView tvTemperature;
	}
}
